package com.gx.smart.building.ui.report

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshActivity
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityFaultRecordBinding
import com.gx.smart.building.ui.report.adapter.FaultRecordAdapter
import com.gx.smart.building.ui.report.viewmodel.FaultReportViewModel
import com.huishi.gx.ib.grpc.lib.ib.app.workorder.v1.dto.WorkOrderDto
import com.scwang.smartrefresh.layout.api.RefreshLayout

class FaultRecordActivity : BaseMVVMRefreshActivity<FaultReportViewModel>(),
    BaseAdapter.OnItemClickListener<WorkOrderDto> {
    private lateinit var adapter: FaultRecordAdapter
    private lateinit var viewBinding: ActivityFaultRecordBinding
    override fun onBindViewModel(): Class<FaultReportViewModel> = FaultReportViewModel::class.java
    override fun onBindLayout(): Int = R.layout.activity_fault_record
    override fun getToolbarTitle(): String = "历史工单"

    override fun initContentView(root: ViewGroup?) {
        viewBinding = ActivityFaultRecordBinding.inflate(layoutInflater, root, true)
    }

    override fun initContent() {
        super.initContent()
        adapter = FaultRecordAdapter(this)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun initData() {
        super.initData()
        viewBinding.refreshLayout.autoRefresh()
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.orderList.observe(this) {
            handleResult(it)
        }
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        viewModel.queryWorkOrderList(currentPage)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        viewModel.queryWorkOrderList(currentPage)
    }

    override fun onItemClick(e: WorkOrderDto, position: Int) {
        val intent = Intent(this, FaultDetailActivity::class.java)
        intent.putExtra("id", e.id)
        startActivity(intent)
    }

}